package com.example.feifei.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 自定义加载框
 * Created by feifei on 2017/7/18.
 */

public class ProgressView extends View {

    //判断绘制方向
    private boolean isPositive = true;
    //绘制起点位置
    private int startOval = -90;
    //绘制弧线的角度
    private int  mProgress = 0;

    private Paint paint;

    public ProgressView(Context context) {
        super(context);
        initPaint();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }
    //初始化画笔
    private void initPaint(){
        paint = new Paint();
        paint.setColor(Color.BLUE);//颜色
        paint.setStrokeWidth(10);//宽度
        paint.setAntiAlias(true);//锯形消除
        paint.setStyle(Paint.Style.STROKE);//设置为空心

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(150,150);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth()/2;
        int radius = center - 10/2;
        setProgress();
        //,left,top,right,bottom分别代表了RectF所在的矩形四条边距离父矩形的距离
        RectF oval = new RectF(center - radius,center - radius,center + radius,center+radius);
        if(isPositive){
            canvas.drawArc(oval,startOval,mProgress,false,paint);
        }else{
            canvas.drawArc(oval,startOval,-mProgress,false,paint);
        }
        postInvalidateDelayed(5);
//        canvas.drawCircle(100,100,50,paint);

    }

    //计算圆环显示进度和起点
    private void setProgress(){
        if(mProgress == 360){
            isPositive = false;
        }else if(mProgress == 0){
            isPositive = true;
        }
        if(isPositive){
            mProgress = mProgress+4;
        }else{
            mProgress = mProgress - 4;
        }
        startOval += 3;
    }
}
